﻿using System.Net;
using Newtonsoft.Json;
using RestSharp;
using xClient.Framework.StaticResource;
using xClient.Framework;
using System;

namespace cMES1007.Services
{
   public class ServiceMethod
    {
        public static RestClient _restClient = new RestClient(StaticDictionary.Dictionary.ServiceUrl);

        public static ExecutionResult<dynamic> GetGroupNameList(string vSection = null)
        {
            ExecutionResult<dynamic> result = new ExecutionResult<dynamic>();
            string api_path = "/basicdata/group/getnamelist";
            try
            {
                var _request = new RestRequest(api_path, Method.GET);
                _request.AddParameter("token", StaticDictionary.Dictionary.Token);
                if (!string.IsNullOrEmpty(vSection))
                {
                    _request.AddQueryParameter("section", vSection);
                }
                IRestResponse _response = _restClient.Execute(_request);
                if (_response.StatusCode == HttpStatusCode.OK)
                {
                    var _content = _response.Content;

                    dynamic _model = JsonConvert.DeserializeObject<dynamic>(_content);

                    result.Status = _model.status;
                    result.Message = _model.message;
                    result.Data = _model.datas;
                    result.ExecuteCount = _model.executeCount;
                }
                else
                {
                    result.Status = false;

                    if (_response.StatusCode == 0)
                    {
                        result.Message = _response.ErrorMessage + ":" + _response.ErrorException;
                    }
                    else
                    {
                        result.Message = "Resource Not Found： " + _response.StatusCode + ":" + _response.StatusDescription + ":";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status = false;
                result.Message = "获取工站信息异常." + ex.Message;
            }

            return result;
        }
        public static ExecutionResult<dynamic> GetSOPInfoList(string vGroup=null,string vSKUName = null)
        {
            var result = new ExecutionResult<dynamic>();
            string api_path = "/esop/getlist";
            try
            {
                var _request = new RestRequest(api_path, Method.GET);
                _request.AddParameter("token", StaticDictionary.Dictionary.Token);
                if (!string.IsNullOrEmpty(vGroup))
                {
                    _request.AddQueryParameter("group", vGroup);
                }
               
                if(!string.IsNullOrEmpty(vSKUName))
                {
                    _request.AddQueryParameter("sku", vSKUName);
                }
                IRestResponse _response = _restClient.Execute(_request);
                if (_response.StatusCode == HttpStatusCode.OK)
                {
                    var _content = _response.Content;

                    dynamic _model = JsonConvert.DeserializeObject<dynamic>(_content);

                    result.Status = _model.status;
                    result.Message = _model.message;
                    result.Data = _model.datas;
                }
                else
                {
                    result.Status = false;

                    if (_response.StatusCode == 0)
                    {
                        result.Message = _response.ErrorMessage + ":" + _response.ErrorException;
                    }
                    else
                    {
                        result.Message = "Resource Not Found： " + _response.StatusCode + ":" + _response.StatusDescription + ":";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status = false;
                result.Message = "获取ESOP信息异常." + ex.Message;
            }

            return result;
        }

        public static ExecutionResult<dynamic> AddESOPInfo(string vModel)
        {
            var result = new ExecutionResult<dynamic>();
            string api_path = "/esop/add";
            try
            {
                var _request = new RestRequest(api_path, Method.POST);
                _request.AddParameter("token", StaticDictionary.Dictionary.Token);
                _request.AddParameter("MODEL", vModel);

                IRestResponse _response = _restClient.Execute(_request);
                if (_response.StatusCode == HttpStatusCode.OK)
                {
                    var _content = _response.Content;

                    dynamic _model = JsonConvert.DeserializeObject<dynamic>(_content);

                    result.Status = _model.status;
                    result.Message = _model.message;
                    result.Data = _model.datas;
                }
                else
                {
                    result.Status = false;

                    if (_response.StatusCode == 0)
                    {
                        result.Message = _response.ErrorMessage + ":" + _response.ErrorException;
                    }
                    else
                    {
                        result.Message = "Resource Not Found： " + _response.StatusCode + ":" + _response.StatusDescription + ":";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status = false;
                result.Message = "新增ESOP信息异常." + ex.Message;
            }

            return result;
        }

        public static ExecutionResult<dynamic> UpdateESOPInfo(string vModel)
        {
            var result = new ExecutionResult<dynamic>();
            string api_path = "/esop/update";
            try
            {
                var _request = new RestRequest(api_path, Method.POST);
                _request.AddParameter("token", StaticDictionary.Dictionary.Token);
                _request.AddParameter("MODEL", vModel);

                IRestResponse _response = _restClient.Execute(_request);
                if (_response.StatusCode == HttpStatusCode.OK)
                {
                    var _content = _response.Content;

                    dynamic _model = JsonConvert.DeserializeObject<dynamic>(_content);

                    result.Status = _model.status;
                    result.Message = _model.message;
                    result.Data = _model.datas;
                }
                else
                {
                    result.Status = false;

                    if (_response.StatusCode == 0)
                    {
                        result.Message = _response.ErrorMessage + ":" + _response.ErrorException;
                    }
                    else
                    {
                        result.Message = "Resource Not Found： " + _response.StatusCode + ":" + _response.StatusDescription + ":";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status = false;
                result.Message = "更新ESOP信息异常." + ex.Message;
            }

            return result;
        }

        public static ExecutionResult<dynamic> DeleteESOPInfo(string vId)
        {
            var result = new ExecutionResult<dynamic>();
            string api_path = $"/esop/delete/{vId}";
            try
            {
                var _request = new RestRequest(api_path, Method.POST);
                _request.AddParameter("token", StaticDictionary.Dictionary.Token);                

                IRestResponse _response = _restClient.Execute(_request);
                if (_response.StatusCode == HttpStatusCode.OK)
                {
                    var _content = _response.Content;

                    dynamic _model = JsonConvert.DeserializeObject<dynamic>(_content);

                    result.Status = _model.status;
                    result.Message = _model.message;
                    result.Data = _model.datas;
                    result.ExecuteCount = _model.ececuteCount;
                }
                else
                {
                    result.Status = false;

                    if (_response.StatusCode == 0)
                    {
                        result.Message = _response.ErrorMessage + ":" + _response.ErrorException;
                    }
                    else
                    {
                        result.Message = "Resource Not Found： " + _response.StatusCode + ":" + _response.StatusDescription + ":";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status = false;
                result.Message = "删除ESOP信息异常." + ex.Message;
            }

            return result;
        }

        public static ExecutionResult<dynamic> UploadFileToServer(string vFile,string vFileType =null)
        {
            var result = new ExecutionResult<dynamic>();
            string api_path = "/fileupload/file/upload";
            try
            {
                var _request = new RestRequest(api_path, Method.POST);
                _request.AddParameter("token", StaticDictionary.Dictionary.Token);

                _request.AddParameter("file_type", String.IsNullOrEmpty(vFileType) ? "ESOP" : vFileType);

                System.IO.FileInfo _tmpFile = new System.IO.FileInfo(vFile);
                if (_tmpFile.Exists)
                {
                    _request.AddFile(_tmpFile.Name, _tmpFile.FullName);
                }

                IRestResponse _response = _restClient.Execute(_request);
                if (_response.StatusCode == HttpStatusCode.OK)
                {
                    var _content = _response.Content;

                    dynamic _model = JsonConvert.DeserializeObject<dynamic>(_content);

                    result.Status = _model.status;
                    result.Message = _model.message;
                    result.Data = _model.datas[0].ToString();
                    result.ExecuteCount = _model.executeCount;
                }
                else
                {
                    result.Status = false;

                    if (_response.StatusCode == 0)
                    {
                        result.Message = _response.ErrorMessage + ":" + _response.ErrorException;
                    }
                    else
                    {
                        result.Message = "Resource Not Found： " + _response.StatusCode + ":" + _response.StatusDescription + ":";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status = false;
                result.Message = "上传图片异常." + ex.Message;
            }

            return result;
        }

        public static ExecutionResult<dynamic> DeleteFileFromServer(string vFile)
        {
            var result = new ExecutionResult<dynamic>();
            string api_path = "/fileupload/file/delete";
            try
            {
                var _request = new RestRequest(api_path, Method.POST);
                _request.AddParameter("token", StaticDictionary.Dictionary.Token);
                _request.AddParameter("file_path", vFile);

                IRestResponse _response = _restClient.Execute(_request);
                if (_response.StatusCode == HttpStatusCode.OK)
                {
                    var _content = _response.Content;

                    dynamic _model = JsonConvert.DeserializeObject<dynamic>(_content);

                    result.Status = _model.status;
                    result.Message = _model.message;
                    result.Data = _model.datas[0].ToString();
                    result.ExecuteCount = _model.executeCount;
                }
                else
                {
                    result.Status = false;

                    if (_response.StatusCode == 0)
                    {
                        result.Message = _response.ErrorMessage + ":" + _response.ErrorException;
                    }
                    else
                    {
                        result.Message = "Resource Not Found： " + _response.StatusCode + ":" + _response.StatusDescription + ":";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status = false;
                result.Message = "上传图片异常." + ex.Message;
            }

            return result;
        }
    }
}
